package com.shoeshopping.dao.joseph;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.shoeshopping.entity.User;
import com.shoeshopping.utils.pc.HibernateUtils;

@Component("userDao")
public class UserDaoImpl implements UserDao{
	
	@Override
	public User getUser(User user) {
		Session session = HibernateUtils.getSession();
		String hql = "from User as u where u.username = :username and u.password = :password";
		Query query = session.createQuery(hql);
		query.setString("username", user.getUsername());
		query.setString("password", user.getPassword());
		List list = query.list();
		if(list!=null && list.size()>0){
			Object obj = list.get(0);
			if(obj instanceof User){
				return (User)obj;
			}
			return null;
		}
		return null;
	}

	@Override
	public boolean saveUser(User user) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		session.saveOrUpdate(user);
		session.getTransaction().commit();
		return true;
	}

	@Override
	public boolean deleteUser(User user) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		session.delete(user);
		session.getTransaction().commit();
		return true;
	}

	@Override
	public boolean updateUser(User user) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		session.saveOrUpdate(user);
		session.getTransaction().commit();
		return true;
	}
}
